package com.itheima.algorithm.sort;

import java.util.Arrays;

/*
选择排序
 */
public class SelectionSort {
    public static void sort(int[] a){
        //1.交换轮数a.length-1；
        //选择交换的索引位置（right）初始a.lenght-1，每次递减
        for(int right=a.length-1;right>0;right--){
            int max=right;
            for(int i=0;i<right;i++){
                if(a[i]>a[max]){
                    max=i;
                }
            }
            if(max!=right){
                swap(a,max,right);
            }
        }
    }

    private static void swap(int [] a,int i,int j){
        int t=a[i];
        a[i]=a[j];
        a[j]=t;
    }

    public static void main(String[] args) {
        int[] a={6,5,4,3,2,1};
        System.out.println(Arrays.toString(a));
        sort(a);
        System.out.println(Arrays.toString(a));
    }
}
